<template>
	<view>
		<view class="bgw wrapper">
			<view class="blank30"></view>
			<view class="flex">
				<u-avatar :text="userInfo.user[1][0]" fontSize="18" randomBgColor size="40"></u-avatar>
				<view class="ml15 flex-1">
					{{userInfo.company[1]}} · {{userInfo.user[1]}}
				</view>
				<view style="width: 150rpx;">
					<u-button type="info" size="mini" class="btn-submit" text="退出登录" @click="logout"></u-button>
				</view>
			</view>
			<view class="blank30"></view>
		</view>
		<view class="blank20"></view>
		<template v-for="(v,i) in menus">
			<menuList :detail="v" @bindTap="menuBindTap"></menuList>
			<view class="blank20"></view>
		</template>

		<template v-if="false">
			<view class="bgw">
				<u-cell-group :border="false">
					<template v-for="v in menu">
						<u-cell :isLink="true" arrow-direction="right" @click="menuNav(v.url)" :border="v.title!='报表'">
							<view class="" slot="title">
								<view class="flex">
									<view class="">
										{{v.title}}
									</view>
									<view class="ml10 text-light-gray font-22">
										{{v.label}}
									</view>
								</view>
							</view>
						</u-cell>
					</template>
				</u-cell-group>
			</view>
			<view class="blank30"></view>
			<view class="bgw">
				<u-cell-group :border="false">
					<u-cell title="关于我们" :isLink="true" arrow-direction="right" :border="false"
						@click="show = !show"></u-cell>
				</u-cell-group>
			</view>
			<view class="blank30"></view>

			<view class="bgw">
				<u-cell-group :border="false">
					<u-cell :isLink="true" arrow-direction="right" :border="false" @click="fleshStorage">
						<view class="" slot="title">
							<view class="flex">
								<view class="">
									更新缓存
								</view>
								<view class="ml10 text-light-gray font-22">
									如发生食材、菜品等数据错误，请更新缓存
								</view>
							</view>
						</view>
					</u-cell>
				</u-cell-group>
			</view>
			<view class="blank30"></view>
			<!-- <view class="wrapper flex">
			<u-button type="primary" shape="circle" class="btn-submit" text="退出登录" @click="logout"></u-button>
		</view> -->
		</template>
		<u-modal :show="show" title="飞米团餐小程序" @confirm="show = !show">
			<view class="" style="text-align: left;">
				<!-- 	<view class="">
					官网：https://fmn315.xyz/
				</view> -->
				<view class="">
					苏州飞米网络科技有限公司 版权所有
				</view>
				<view class="blank20"></view>
				<view class="">
					ICP证：苏ICP备2020052433号-3X
				</view>
			</view>
		</u-modal>
	</view>
</template>

<script>
	import menuList from "./components/menu-list.vue";
	import {
		material_static_data,
		dish_material_common_data
	} from '@/common/api.js';
	export default {
		components: {
			menuList
		},
		data() {
			return {
				role: null,
				userInfo: null,
				show: false,
				menu: [{
						title: '采购',
						label: '已下单，待审批，增加采购单',
						url: '/pages/purchase/purchase'
					},
					{
						title: '库存',
						label: '入库，出库，盘点',
						url: '/pages/stock/stock'
					},
					{
						title: '报表',
						label: '销售、成本',
						url: '/pages/report/report'
					}
				],
				menus:[
					{
						title: '报表',
						desc: '销售、分餐、成本',
						list: [
							{
								title: '菜品计划',
								icon: "/static/images/menu-distribution.png",
								url: '/pagesA/dishPlan/dishPlan',
								auth: 1
							}
						]
					}
				],
				menus_bak: [{
						title: '采购',
						desc: '已下单，待审批，增加采购单',
						list: [{
								title: '采购单',
								icon: "/static/images/menu-purchase.png",
								url: '/pages/purchase/purchase',
								auth: 1
							},
							{
								title: '新增采购',
								icon: "/static/images/menu-purchase-add.png",
								url: '/pagesA/food/food',
								auth: 1
							}
						]
					},
					{
						title: '库存',
						desc: '入库，出库，盘点',
						list: [{
								title: '库存总览',
								icon: "/static/images/menu-stock.png",
								url: '/pages/stock/stock',
								auth: 1
							},
							{
								title: '入库',
								icon: "/static/images/menu-stock-in.png",
								url: '/pagesA/warehousing/warehousing',
								auth: 1
							},
							{
								title: '出库',
								icon: "/static/images/menu-stock-out.png",
								url: '/pagesA/stockOut/stockOut',
								auth: 1
							},
							{
								title: '盘点',
								icon: "/static/images/menu-inventory.png",
								url: '/pagesA/inventory/inventory',
								auth: 1
							}
						]
					},
					{
						title: '报表',
						desc: '销售、分餐、成本',
						list: [
							{
								title: '菜品计划',
								icon: "/static/images/menu-distribution.png",
								url: '/pagesA/dishPlan/dishPlan',
								auth: 1
							},
							{
								title: '月销售',
								icon: "/static/images/menu-report.png",
								url: '/pages/report/report',
								auth: 1
							},
							{
								title: '语音录入',
								icon: "/static/images/menu-report-audio.png",
								url: '/pagesA/saleRecorder/saleRecorder',
								auth: 1
							},
							{
								title: '分餐录入',
								icon: "/static/images/menu-distribution.png",
								url: '/pagesA/distributionRecorder/distributionRecorder',
								auth: 1
							},
							{
								title: '成本录入',
								icon: "/static/images/menu-cost.png",
								url: '/pages/report/cost',
								auth: 1
							},
							{
								title: '成本总览',
								icon: "/static/images/menu-cost.png",
								url: '/pages/report/cost?isRatio=1',
								auth: 1
							},
							{
								title: '流程检查',
								icon: "/static/images/menu-workflow.png",
								url: '/pagesA/workflow/workflow',
								auth: 1
							}
						]
					},
					{
						title: '设置',
						desc: '',
						list: [{
								title: '关于我们',
								icon: "/static/images/menu-aboutus.png",
								url: '',
								tap: 'aboutUs',
								auth: 1
							},
							{
								title: '更新缓存',
								icon: "/static/images/menu-fresh.png",
								url: '',
								tap: 'fleshStorage',
								auth: 1
							},
						]
					}
				]
			}
		},
		methods: {
			logout() {
				this.$store.dispatch('logout');
				uni.reLaunch({
					url: '/pages/login/login'
				});
			},
			menuNav(url) {
				uni.switchTab({
					url: url
				});
			},
			async fleshStorage() {
				uni.showLoading({
					title: '更新缓存'
				})
				try {
					await this.get_dish_material_common_data();
					await this.get_material_static_data();

					uni.$u.toast('更新缓存成功');
				} catch (e) {
					uni.$u.toast(e);
					//TODO handle the exception
				}
			},
			aboutUs() {
				this.show = true;
			},
			menuBindTap(key) {
				if (key == 'aboutUs') {
					this.aboutUs();
				}
				if (key == 'fleshStorage') {
					this.fleshStorage();
				}
			}
		},
		onShow() {
			try {
				this.role = this.$store.getters.get_role;
				this.userInfo = this.$store.getters.get_userInfo;
				let write_cus_loc = this.role['报表模块']['销售'].write;
				let stockAuth = this.role['仓管模块'];
				// console.log(stockAuth);
				// stockAuth['出库'].write = false;
				this.menus.forEach((menu, i) => {
					menu.list.forEach(v => {
						if (v.title == '出库') {
							v.auth = stockAuth['出库'].write || stockAuth['出库申请'].write;
						}
						if (v.title == '入库') {
							v.auth = stockAuth['入库'].write;
						}
						if (v.title == '盘点') {
							v.auth = stockAuth['盘点'].write;
						}
						if (v.title == '新增采购') {
							v.auth = this.role['采购模块'].is_purchaser;
						}
						if (v.title == '语音录入' || v.title == '分餐录入' || v.title == '菜品计划') {
							v.auth = write_cus_loc.length > 0;
						}
					})
				})
			} catch (e) {
				uni.reLaunch({
					url: '/pages/login/login'
				});
			}
		},
		onLoad(d) {

		},
	}
</script>

<style>

</style>